#!/bin/sh

# $OpenBSD: update-patches,v 1.1 2010/08/20 14:50:36 espie Exp $
# Copyright (c) 2000
# Marc Espie.  All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Neither the name of OpenBSD nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY ITS AUTHOR AND THE OpenBSD project ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# Keywords expanded by CVS
keywords="Author|CVSHeader|Date|Header|Id|Name|Locker|Log|RCSfile|Revision"
keywords="$keywords|Source|State|OpenBSD"

# Find out all $PATCHORIG files and strip the name to what diff will use
cd "$WRKDIST" && find . -type f -name '*'${PATCHORIG} | \
    fgrep -v Oops.rej.orig | \
    fgrep -v $DISTORIG | \
    sed -e "s,^./\(.*\)\.${PATCHORIG#.*}\$,\1," | {
while read file 
do
    if cmp "$WRKDIST/$file" "$WRKDIST/$file$PATCHORIG" >/dev/null 2>&1; then
    	echo 1>&2 "$file and $file$PATCHORIG are identical"
    	continue
    fi
    if ! [ -f "$WRKDIST/$file" ]; then
		echo 1>&2 "$file does not exist"
		continue
	fi
    echo 1>&2 "Processing $file"
    # look in patchdir for an existing patchfile matching this
    mkdir -p $PATCHDIR
    cd $PATCHDIR
    for i in $PATCH_LIST
    do
	# Ignore non-files, or old backup
	[ -f $i ] || continue
	case $i in \
	*.orig|*.rej|*~) ;;
	*) # Patch found. Is this the one ?
	    if grep "^--- $file$PATCHORIG" $i >/dev/null
	    then
		accounted="$accounted $i"
		# found it, splice before diff part with diff
		esc=`echo $file | sed -e 's,/,\\\\/,g'`
		{ sed -e "/^--- $esc$PATCHORIG/,\$ d" <$i
		    (cd "$WRKDIST" && diff -u -p -a ${DIFF_ARGS} "$file$PATCHORIG" "$file") } >$i.new
		# did it change ? mark it as changed
		if diff ${DIFF_ARGS} -u \
			-I "^--- `echo $file$PATCHORIG | sed 's/[.+]/\\\\&/g'`	" \
			-I "^\+\+\+ `echo $file | sed 's/[.+]/\\\\&/g'`	" $i $i.new 1>&2
		then
		    rm $i.new
		else
		    echo 1>&2 "Patch $i for $file updated"
		    mv $i $i.orig
		    mv $i.new $i
		    edit="$edit $i"
		fi
		continue 2
	    fi;;
	esac
    done
    # Build a sensible name for the patch file
    patchname=patch-`echo $file|sed -e s,[\ /.],_,g`
    echo 1>&2 "No patch-* found for $file, creating $patchname"
    { echo "$"OpenBSD$; \
	(cd "$WRKDIST" && diff -u -p -a ${DIFF_ARGS} "$file$PATCHORIG" "$file") }>$patchname
    edit="$edit $patchname"
    accounted="$accounted $patchname"
done

# Verify all patches accounted for
if cd $PATCHDIR 2>/dev/null; then
    for i in *
    do
	[ -f $i ] || continue
	case $i in \
	*.orig|*.rej|*~) ;;
	*)
	    if grep '^\\ No newline at end of file' $i >/dev/null; then
		    echo 1>&2 "*** Patch $i needs manual intervention"
	    fi
	    for j in $accounted 
	    do
		if [ $j = $i ]
		then
		    continue 2
		fi
	    done
	    echo 1>&2 "*** Patch $i not accounted for.";;
	esac
    done
else
    echo 1>&2 "No patch found"
fi

# Check for $Id: update-patches,v 1.1 2010/08/20 14:50:36 espie Exp $ and similar bugs in all those patch files.
for i in $accounted
do
    if sed -e '1,/^---/ d' $i|egrep -q '\$('"$keywords"').*\$'
    then
	    echo 1>&2 "Problem with $i: CVS keyword found in patch"
    fi
done

echo $edit
}
exit 0
